<template>
  <div id="app">
    <el-container>
      <el-row>规格管理</el-row>
      <el-main>
        <el-row>
          <el-col :span="12">
            <el-button type="success" plain @click="editSpecification" icon="el-icon-plus">新建</el-button>
            <el-button>删除</el-button>
            <el-button>刷新</el-button>
          </el-col>
          <el-col :span="12">
            <!--条件查询-->
            <el-form :inline="true" size="medium" :model="speciifcatonSearch" class="demo-form-inline">
              <el-form-item >规格名称</el-form-item>
              <el-form-item >
                <el-input v-model="speciifcatonSearch.specName" ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" plain @click="search" icon="el-icon-user">查询</el-button>
              </el-form-item>
            </el-form>
          </el-col>

        </el-row>
        <el-table
            :data="specificationList"
            style="width: 100%">
          <el-table-column
              width="180">
          </el-table-column>
          <el-table-column
              prop="id"
              label="规格id"
              width="180">
          </el-table-column>
          <el-table-column
              prop="specName"
              label="规格名称"
              width="180">
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>

        <!--对话框-->
        <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
          {{specification}}
          <el-form :model="specification">
            <el-form-item label="规格名称" :label-width="formLabelWidth">
              <el-input v-model="specification.specName" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveSpecification">确 定</el-button>
          </div>
        </el-dialog>
      </el-main>
    </el-container>
  </div>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

nav {
  padding: 30px;
}

nav a {
  font-weight: bold;
  color: #2c3e50;
}

nav a.router-link-exact-active {
  color: #42b983;
}
</style>

<script>
import specificationApi from "@/api/specification";
import specificationOptionApi from "@/api/specificationOption";
export default {
  data() {
    return {
      specificationList: [],
      speciifcatonSearch: {},
      specification: {},
      page:1,               // 当前页
      pageSize:5,           // 每页大小
      total:100,            // 总记录数
      dialogFormVisible:false,  // 控制对话框的显示隐藏
      formLabelWidth:'100px',   // 对话框提示文本的宽度
      title: '添加specificationOption',          // 对话框标题
    }
  },
  created() {
    this.findSpecification()
    this.search()

  },
  methods: {
    async findSpecification(){
      const {data:res} = await specificationApi.findSpecification()
      this.specificationList = res
    },
    async search(){
      const {data} = await specificationApi.search(this.page,this.pageSize,this.speciifcatonSearch)
      this.specificationList = data.rows
      this.total = data.total
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    // 每页大小改变时事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    },
    async saveSpecification(){
      const {data:res} = await specificationApi.save(this.specification)
      if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 重新刷新页面
        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },
    editSpecification(){
      this.dialogFormVisible = true
    }
  }
}
</script>
